//
//  NSBundle+Extension.swift
//  微博Swift
//
//  Created by  justinchou on 16/2/26.
//  Copyright © 2016年  justinchou. All rights reserved.
//

import UIKit

extension NSBundle {
    
    class var justin_currentversion: String {
        //判断是否有版本号，没有默认设置成000.000.000
        guard let currentVersion = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String else {
            return "000.000.000"
        }
        
        //将版本号格式化（xxx.xxx.xxx）
        //拆分版本号
        let array = currentVersion.componentsSeparatedByString(".")
        //符合c语言的类型:CVarArgType
        var args: [CVarArgType] = [0, 0, 0]
        
        for e in array.enumerate() {
            //错误之处，e.element是string，需要将其转变为int，否则显示会是一个很大的数（乱码？）
//            args.insert(e.element ?? 0, atIndex: e.index)
            args.insert(Int(e.element) ?? 0, atIndex: e.index)
        }
//        String(format: "%03d.%03d.%03d", [1, 2, 3])
        //ar
        let versionString = String(format: "%03d.%03d.%03d", arguments: args)
        print("\(versionString)")
        
        return versionString
    }
    
    
    //直接获取启动图片
    class var justin_launchImageView: UIImage? {
        
        guard let images = NSBundle.mainBundle().infoDictionary?["UILaunchImages"] as? [[String: String]] else {
            return nil
        }
        
        // 根据屏幕尺寸过滤数组
        let result = images.filter { (dict) -> Bool in
            let imageSize = CGSizeFromString(dict["UILaunchImageSize"]!)
            let orientation = dict["UILaunchImageOrientation"]!
            
            return orientation == "Portrait" && CGSizeEqualToSize(imageSize, UIScreen.mainScreen().bounds.size)
        }
        
        // 获取图像名称
        guard let imageName = result.first?["UILaunchImageName"] else {
            return nil
        }
        
        return UIImage(named: imageName)
    }
}